package com.blockmeta.bbs.businesslibrary.util;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import androidx.annotation.LayoutRes;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.PermissionUtils.OnRationaleListener.ShouldRequest;
import com.blockmeta.bbs.baselibrary.base.adapter.BaseAdapter;
import com.blockmeta.bbs.baselibrary.base.app.BaseApp;
import com.blockmeta.bbs.baselibrary.utils.SharedPreferenceUtil;
import com.blockmeta.bbs.baselibrary.utils.ToastUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.adapter.RankChoiceAdapter;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.dialog.poupwindow.LocationPopupWindow;
import com.blockmeta.bbs.businesslibrary.launch.navigationYMAL;
import com.blockmeta.bbs.businesslibrary.pojo.MainTabItemBasePOJO;
import com.blockmeta.bbs.businesslibrary.pojo.MainTabPOJO;

import java.util.ArrayList;
import java.util.List;


/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/01/10
 *     desc  : 对话框工具类
 * </pre>
 */
public class DialogUtil {

    public static void showRationaleDialog(final ShouldRequest shouldRequest) {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (topActivity == null) return;
        new AlertDialog.Builder(topActivity)
                .setTitle(android.R.string.dialog_alert_title)
                .setMessage(R.string.permission_rationale_message)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        shouldRequest.again(true);
                    }
                })
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        shouldRequest.again(false);
                    }
                })
                .setCancelable(false)
                .create()
                .show();

    }

    public static void showOpenAppSettingDialog() {
        Activity topActivity = ActivityUtils.getTopActivity();
        if (topActivity == null) return;
        new AlertDialog.Builder(topActivity)
                .setTitle(android.R.string.dialog_alert_title)
                .setMessage(R.string.permission_denied_forever_message)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        PermissionUtils.openAppSettings();
                    }
                })
                .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .setCancelable(false)
                .create()
                .show();
    }

    private static AlertDialog sAlertDialog;

    private static void createDialog(Context context) {
        createDialog(context, R.style.NoDimDialog, false);
    }

    private static void createDialog(Context context, boolean needCreateNew) {
        createDialog(context, R.style.NoDimDialog, needCreateNew);
    }

    private static void createDialog(Context context, @StyleRes int theme, boolean needCreateNew) {
        if (sAlertDialog == null || needCreateNew) {
            sAlertDialog = new AlertDialog.Builder(context, theme).create();
        }
        sAlertDialog.show();
    }
    /**
     * 情况说明弹窗
     */
    public static void showExplainDialog(Context context, @LayoutRes int layout) {
        createDialog(context, R.style.AlertDialog, false);
        sAlertDialog.setCancelable(false);
        Window window = sAlertDialog.getWindow();
        if (window == null) return;
        window.setContentView(layout);

        Button sure = window.findViewById(R.id.btn_sure);

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sAlertDialog.dismiss();
            }
        });
    }

    public static void showAllBBSBlockPopup(Context mContext, View titleView, ImageView img, ClickAdapterCallBack clickAdapterCallBack) {


        View contentView = LayoutInflater.from(mContext).inflate(R.layout.popup_choice_bbs_left, null);

        //综合节点
        RecyclerView recyAll = (RecyclerView) contentView.findViewById(R.id.left_recy_all);
        //项目节点
        RecyclerView recyMatket = (RecyclerView) contentView.findViewById(R.id.left_market);
        //技术节点
        RecyclerView recyWallet = (RecyclerView) contentView.findViewById(R.id.left_recy_wallet);
        //平台节点
        RecyclerView recyOther = (RecyclerView) contentView.findViewById(R.id.left_recy_other);

        final PopupWindow popupWindow = LocationPopupWindow.initRank(mContext, contentView, titleView, img);

        GridLayoutManager manager1 = new GridLayoutManager(mContext, 4);
        manager1.setOrientation(LinearLayoutManager.VERTICAL);
        GridLayoutManager manager2 = new GridLayoutManager(mContext, 4);
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        GridLayoutManager manager3 = new GridLayoutManager(mContext, 4);
        manager3.setOrientation(LinearLayoutManager.VERTICAL);
        GridLayoutManager manager4 = new GridLayoutManager(mContext, 4);
        manager4.setOrientation(LinearLayoutManager.VERTICAL);

        List<MainTabPOJO.ChainnodeBean> chainnode = navigationYMAL.getsMainTabPOJO().getChainnode();

        //综合节点
        List<MainTabItemBasePOJO> listAll = new ArrayList<>();
        //项目节点
        List<MainTabItemBasePOJO> listMarket = new ArrayList<>();
        //技术节点
        List<MainTabItemBasePOJO> listWallet = new ArrayList<>();
        //平台节点
        List<MainTabItemBasePOJO> listOther = new ArrayList<>();
        for (int i = 0; i < chainnode.size(); i++) {

            if (chainnode.get(i).getId()==61){
                //综合节点
                List<MainTabPOJO.ChainnodeBean.SubBean> sub = chainnode.get(i).getSub();
                for (int j = 0; j < sub.size(); j++) {
                    MainTabItemBasePOJO pojo = new MainTabItemBasePOJO();
                    MainTabPOJO.ChainnodeBean.SubBean subBean = sub.get(j);
                    pojo.setName(subBean.getName());
                    pojo.setImg(subBean.getImg());
                    listAll.add(pojo);
                }
            }
            if (chainnode.get(i).getId()==182){
                //项目节点
                List<MainTabPOJO.ChainnodeBean.SubBean> sub = chainnode.get(i).getSub();
                for (int j = 0; j < sub.size(); j++) {
                    MainTabItemBasePOJO pojo = new MainTabItemBasePOJO();
                    MainTabPOJO.ChainnodeBean.SubBean subBean = sub.get(j);
                    pojo.setName(subBean.getName());
                    pojo.setImg(subBean.getImg());
                    listMarket.add(pojo);
                }
            }
            if (chainnode.get(i).getId()==43){
                //技术节点
                List<MainTabPOJO.ChainnodeBean.SubBean> sub = chainnode.get(i).getSub();
                for (int j = 0; j < sub.size(); j++) {
                    MainTabItemBasePOJO pojo = new MainTabItemBasePOJO();
                    MainTabPOJO.ChainnodeBean.SubBean subBean = sub.get(j);
                    pojo.setName(subBean.getName());
                    pojo.setImg(subBean.getImg());
                    listWallet.add(pojo);
                }
            }
            if (chainnode.get(i).getId()==183){
                //平台节点！
                List<MainTabPOJO.ChainnodeBean.SubBean> sub = chainnode.get(i).getSub();
                for (int j = 0; j < sub.size(); j++) {
                    MainTabItemBasePOJO pojo = new MainTabItemBasePOJO();
                    MainTabPOJO.ChainnodeBean.SubBean subBean = sub.get(j);
                    pojo.setName(subBean.getName());
                    pojo.setImg(subBean.getImg());
                    listOther.add(pojo);
                }
            }


        }
        recyAll.setLayoutManager(manager1);
        recyMatket.setLayoutManager(manager2);
        recyWallet.setLayoutManager(manager3);
        recyOther.setLayoutManager(manager4);

        RankChoiceAdapter adapterAll = new RankChoiceAdapter(R.layout.item_rankchoice, listAll, mContext);
        RankChoiceAdapter adapterMarkrt = new RankChoiceAdapter(R.layout.item_rankchoice, listMarket, mContext);
        RankChoiceAdapter adapterWallet = new RankChoiceAdapter(R.layout.item_rankchoice, listWallet, mContext);
        RankChoiceAdapter adapterOther = new RankChoiceAdapter(R.layout.item_rankchoice, listOther, mContext);

        recyAll.setAdapter(adapterAll);
        recyMatket.setAdapter(adapterMarkrt);
        recyWallet.setAdapter(adapterWallet);
        recyOther.setAdapter(adapterOther);
        /**
         * 具体处理点击时间
         */
        clickAdapterCallBack.ClickAdapterCallBack(popupWindow, adapterAll, adapterMarkrt, adapterWallet, adapterOther
                ,recyAll,recyMatket,recyWallet,recyOther);
    }

    public interface ClickAdapterCallBack {
        void ClickAdapterCallBack(PopupWindow popupWindow, BaseAdapter adapter1, BaseAdapter adapter2,
                                  BaseAdapter adapter3, BaseAdapter adapter4, RecyclerView recyAll, RecyclerView recyMatket, RecyclerView recyWallet, RecyclerView recyOther);
    }

    public interface OnDialogClickListener {
        void onDialogClick(int id);

    }

    /**
     * 确认框弹窗
     */
    public static void showConfirmDialog(Context context, @LayoutRes int layout, String confirmContent, final OnDialogClickListener onDialogClickListener) {
        createDialog(context, R.style.AlertDialog, false);
        sAlertDialog.setCancelable(false);
        Window window = sAlertDialog.getWindow();
        if (window == null) return;
        window.setContentView(layout);
        TextView tvHint = window.findViewById(R.id.tv_confirm_content);
        tvHint.setText(confirmContent);
        Button sure = window.findViewById(R.id.btn_sure);
        Button cancel = window.findViewById(R.id.btn_cancel);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.btn_cancel) {
                    sAlertDialog.dismiss();
                } else if (onDialogClickListener != null) {
                    onDialogClickListener.onDialogClick(view.getId());
                    sAlertDialog.dismiss();
                }
            }
        };
        cancel.setOnClickListener(onClickListener);
        sure.setOnClickListener(onClickListener);

    }

    /**
     * 更新提示框
     */
    public static void showUpdate(Context context, final OnDialogClickListener onDialogClickListener) {
        createDialog(context, true);
        sAlertDialog.setCancelable(false);
        Window window = sAlertDialog.getWindow();
        if (window == null) return;
        window.setContentView(R.layout.view_app_update);

        TextView content = (TextView) window.findViewById(R.id.tv_content);
        content.setText(SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.UPDATE_DES, BaseApp.getApp().getResources().getString(R.string.sys_update_info)));
        Button cancel = (Button) window.findViewById(R.id.btn_cancel);
        Button sure = (Button) window.findViewById(R.id.btn_sure);
        ProgressBar progressBar = (ProgressBar) window.findViewById(R.id.down_progressbar);

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btn_cancel) {
                    sAlertDialog.dismiss();
                    return;
                }
                if (onDialogClickListener != null) {
                    sAlertDialog.dismiss();
                    ToastUtil.showLong("后台下载中");
                    onDialogClickListener.onDialogClick(R.id.btn_sure);
                }
            }
        };
        cancel.setOnClickListener(onClickListener);
        sure.setOnClickListener(onClickListener);
    }

    /**
     * 强制更新提示框
     */
    public static AlertDialog showMustUpdate(Context context, final OnDialogClickListener onDialogClickListener) {
        createDialog(context);
        sAlertDialog.setCancelable(false);
        Window window = sAlertDialog.getWindow();
        if (window == null) return null;
        window.setContentView(R.layout.view_app_update);

        TextView content = (TextView) window.findViewById(R.id.tv_content);
        content.setText(SharedPreferenceUtil.getString(BaseApp.getApp(), Constant.UPDATE_DES, BaseApp.getApp().getResources().getString(R.string.sys_update_info)));
        final Button cancel = (Button) window.findViewById(R.id.btn_cancel);
        final Button sure = (Button) window.findViewById(R.id.btn_sure);
        final View divider = window.findViewById(R.id.v_divider_vertical);
        final ProgressBar progressBar = (ProgressBar) window.findViewById(R.id.down_progressbar);
        cancel.setVisibility(View.GONE);
        divider.setVisibility(View.GONE);
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onDialogClickListener != null) {
                    sure.setVisibility(View.INVISIBLE);
                    cancel.setVisibility(View.INVISIBLE);
                    divider.setVisibility(View.INVISIBLE);
                    progressBar.setVisibility(View.VISIBLE);
                    onDialogClickListener.onDialogClick(R.id.btn_sure);
                }
            }
        };
        sure.setOnClickListener(onClickListener);
        return sAlertDialog;
    }
}
